package com.dk.controller;

import com.dk.pojo.User;
import com.dk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("login")
public class LoginController {
    @Autowired
    private UserService userService;

    @RequestMapping("checklogin")
    @ResponseBody
    public Map<String,Object> checklogin(User u, HttpServletRequest req){
        List<User> list = userService.getUserByName(u.getUsername());
        Map<String,Object> result = new HashMap<>();
        if(list.size() <= 0){
            result.put("code",false);
            result.put("message","账号不存在");
            return result;
        }

        User user = list.get(0);
        if(u.getPassword() == null || "".equals(u.getPassword()) || !user.getPassword().equals(u.getPassword())){
            result.put("code",false);
            result.put("message","密码不正确");
            return result;
        }

        result.put("code",true);
        result.put("message","登陆成功");
        req.getSession().setAttribute("user",user);
        return result;
    }

    @RequestMapping("toTreeview")
    public String toTreeview(){
        return "/treeview/treeview";
    }
}
